package com.example.mydemo;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.junit.Test;
import org.springframework.util.Assert;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.net.SocketTimeoutException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @Description: 不启动spring
 * @Param:
 * @return:
 * @Author: PengCheng Tang
 * @Date: 2020/5/27
 */
public class AES2 {

    // 加密
    public static String encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
//        if (sKey.length() != 16) {
//            System.out.print("Key长度不是16位");
//            return null;
//        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }

    // 解密
    public static String decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
//            if (sKey.length() != 16) {
//                System.out.print("Key长度不是16位");
//                return null;
//            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        String content = "{\n" +
                "    \"userId\": 15378206591,\n" +
                "    \"ruleType\": 11,\n" +
                "    \"intergral\": 10\n" +
                "}";
        String password = "15378206591cqmyg";
        System.out.println("需要加密的内容：" + content);
        String hexStr = encrypt(content, password);
        System.out.println("加密后的16进制密文:" + hexStr);
        String decrypt = decrypt(hexStr, password);
        System.out.println("解密后的内容：" + decrypt);
        Map<String, String> map = JSONObject.parseObject(decrypt, Map.class);
        System.out.println(map.toString());


        JSONObject json = new JSONObject();
        //这里是跟php约定好的用手机号加上“床前明月光”首字母作为加解密密钥，利用aes堆成加密算法
        String paramString = decrypt("5XYaxTGqHNLWjjWNG71F2QFZUrbfIsN4mg8MZl268T9p+QwpOZCSTyNzfT8Ufc/SrNm4eYINqNdRmzT18bLMmn+fSDcPHdyWCxx9t9g/4tc=", "15378206591cqmyg");
        Map<String, Object> map3 = JSONObject.parseObject(paramString, Map.class);
        json.put("ruleType", (Integer) map3.get("ruleType"));
    }


    @Test
    public void test() {
        String s = "<p style=\"text-align:center\"><img src=\"http://file.youxian.gov.cn/ftp/suobei/media/2020-02-25/d50ebe3145954ae287a367802dc979bc/bDgPhvXNwr.jpg\" _src=\"http://file.youxian.gov.cn/ftp/suobei/media/2020-02-25/d50ebe3145954ae287a367802dc979bc/bDgPhvXNwr.jpg\"></p>";

        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> pics = new ArrayList<String>();
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(s);
        while (m_image.find()) {
            img = m_image.group();
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                if (m.group(1).contains("/sctv")) {
                    pics.add(m.group(1));
                }
            }
        }

        System.out.println(pics.toString());

    }


    //断言测试
    @Test
    public void test2() {
        System.out.println("hahahaha");
        try {
            Assert.state(false, "fuck u asshole");
        } catch (Exception e) {
            System.out.println(ExceptionUtils.getStackTrace(e) + e.getMessage());
        }
        System.out.println("bababa");
    }


    private int anInt;
    private String string;
    private Integer ig;

    @Test
    public void tests() {
        System.out.println(test3());

    }

    public String test3() {

        File file = new File("D:\\ideaWorkspaceAndProjects\\bazhou");
        System.out.println(file.getName());
        try {
            int a=1/ 0;
            return "a";

        } catch (Exception e) {
            return "fsdf";
        }
        finally {
            System.out.println("test");
        }
    }


    @Test
    public void test4() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(12);
        list.add(4);
        list.add(3);
        list.add(5);
//将集合按照默认的规则排序,按照数字从小到大的顺序排序
        Collections.sort(list);
        System.out.println("list = " + list);
        System.out.println("===================");
//将集合中的元素反转
        Collections.reverse(list);
        System.out.println("list = " + list);
//addAll方法可以往集合中添加元素，也可往集合中添加一个集合
        Collections.addAll(list, 9, 20, 56);
//打乱集合中的元素
        Collections.shuffle(list);
        System.out.println("list = " + list);

//Arrays.asList方法可以返回一个长度内容固定的List集合
        List<String> list2 = Arrays.asList("tom", "kobe", "jordan", "tracy", "westbook", "yaoming", "ace", "stephen");
//按照字符串首字符的升序排列
        System.out.println(list2);
        Collections.sort(list2);
        System.out.println("list2 = " + list2);
//这里是是使用匿名内部类来完成的
        Collections.sort(list2, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //指定比较规则，按照首字母降序来排列
                return o2.charAt(0) - o1.charAt(0);
            }
        });
        System.out.println("list2 = " + list2);
//这里是使用lambda表达式来完成
        Collections.sort(list2, (o1, o2) -> {
            return o2.charAt(o2.length() - 1) - o1.charAt(o1.length() - 1);
        });
    }


}


